<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        function getXHR() {
            var xhr;
            if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            } else {
                xhr = new ActiveXObjext('Microsoft.XMLHttp');
            }
            return xhr;
        }
        //第一步：创建XMLHttpReques对象
        var ajax = getXHR();

        //第二步：创建请求
        ajax.open("post", "http://localhost:8000", true);//open发送数据一定是GET请求
       // ajax.open("get", "http://localhost:8000?username=小红&password=1111", true);

        //第三步：发送请求
        //ajax.send(null);
        ajax.send("12345");//send发送数据一定是POST请求

        //第四步：接受响应执行回调函数——当状态改变时自动触发该函数
        ajax.onreadystatechange = function () {
            console.log(ajax.readyState);
            if (ajax.readyState == 4 && ajax.status == 200) { //接受数据成功条件
                var txt = ajax.responseText;
                console.log(typeof txt,txt);
                // var data = JSON.parse(txt); //json数据解析为js对象
                //console.log(typeof data);

                //其他DOM操作
            }
        }
    </script>
</body>

</html>
</script>
</body>

</html>